package com.jike.noobmoney.mvp.view.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.ChannelsTaskAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.CateListEntity;
import com.jike.noobmoney.mvp.presenter.TaskPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by kekex on 2018/8/21.
 * 任务大厅
 */
public class TaskAllFragment extends BaseNewFragment implements IView {

    @BindView(R.id.tv_center_title)
    TextView tvTitle;
    @BindView(R.id.channelTab)
    TabLayout channelTab;
    @BindView(R.id.viewpger)
    ViewPager viewpger;


    private TaskPresenter taskPresenter;
    private List<CateListEntity.CatelistBean> mData = new ArrayList<>();
    private List<Fragment> newsFragmentList = new ArrayList<>();

    @Override
    protected int layoutResID() {
        return R.layout.fragment_task_all;
    }


    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        mHasLoadedOnce = true;
        taskPresenter = new TaskPresenter(this);
        initData();
    }

    protected void initData() {
        tvTitle.setText("赚钱大厅");
        taskPresenter.getCatelist(ConstantValue.RequestKey.task_type);
    }

    /**
     * 加载tab
     */
    private void initView() {
        if (mData != null && mData.size() > 0) {
            for (int i = 0; i < mData.size(); i++) {
                TaskListFragment newsListFragment = new TaskListFragment();
                Bundle bundle = new Bundle();
                bundle.putString(TaskListFragment.Channels_id, "" + mData.get(i).getC_id());
                newsListFragment.setArguments(bundle);
                newsFragmentList.add(newsListFragment);
                channelTab.addTab(channelTab.newTab().setText(mData.get(i).getCatename()));
            }

            ChannelsTaskAdapter adapter = new ChannelsTaskAdapter(getChildFragmentManager(), mData, newsFragmentList);
            viewpger.setAdapter(adapter);
            channelTab.setupWithViewPager(viewpger);
            viewpger.setOffscreenPageLimit(mData.size());

            viewpger.setCurrentItem(0);
        }

    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if (ConstantValue.RequestKey.task_type.equals(actionType)) {
            List<CateListEntity.CatelistBean> catelist = (List<CateListEntity.CatelistBean>) data;

            if (catelist != null && catelist.size() > 0) {
                mData.clear();
                mData.addAll(catelist);
                initView();
            }

        }
    }


    @Override
    public void onFailed(String code, String msg) {
        Logger.e("xuke", "msg=" + msg);
    }

}
